package com.wengzw.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wengzw.po.Customer;
import com.wengzw.service.CustomerService;
import com.wengzw.utils.DataGridViewResult;
import com.wengzw.vo.CustomerVo;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/Client")
public class CustomerController {
    @Resource
    private CustomerService customerService;
    @RequestMapping("/list")
    public DataGridViewResult clientList(CustomerVo customerVo) {
        //Map<String, Object> map = new HashMap<String, Object>();
        //设置分页信息(当前页码，每页显示数量）
        PageHelper.startPage(customerVo.getPage(),customerVo.getLimit());
//        System.out.print(customerVo.getPage());
//        System.out.print(customerVo.getLimit());
        //调用分页查询账单列表的方法

        List<Customer> customerList = customerService.findCustomerList(customerVo);
        //创建分页对象
        PageInfo<Customer> pageInfo = new PageInfo<Customer>(customerList);
        //将数据表格返回的key保存到map集合中
//        map.put("code",0);
//        map.put("msg","");
//        map.put("count",pageInfo.getTotal());//总数量
//        map.put("data",pageInfo.getList()); //获取列表
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    //添加客户
    @RequestMapping("/addcustomer")
    public String addCustomer(Customer customer){
        Map<String,Object> map = new HashMap<String, Object>();
        if(customerService.addCustomer(customer)>0){
            map.put("success",true);
            map.put("message","添加成功");
        }else{
            map.put("success",false);
            map.put("message","添加失败");
        }
        return JSON.toJSONString(map);
    }

    //修改客户
    @RequestMapping("/updatecustomer")
    public String updateCustomer(Customer customer){
        Map<String,Object> map = new HashMap<String, Object>();
        if(customerService.updateCustomer(customer)>0){
            map.put("success",true);
            map.put("message","修改成功");
        }else{
            map.put("success",false);
            map.put("message","修改失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 删除客户
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        if(customerService.deleteById(id)>0){
            map.put("success",true);
            map.put("message","删除成功");
        }else{
            map.put("success",false);
            map.put("message","删除失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @RequestMapping("/batchDelete")
    public String batchDelete(String ids){
        System.out.println(ids);
        Map<String,Object> map = new HashMap<String,Object>();
        int count = 0;
        //将字符串拆分成数组
        String[] idsStr = ids.split(",");
        for (int i = 0; i < idsStr.length; i++) {
            count = customerService.deleteById(Integer.valueOf(idsStr[i]));
            if(count>0){
                map.put("success",true);
                map.put("message","删除成功");
            }
        }
        //判断受影响行数是否为0
        if(count<=0){
            map.put("success",false);
            map.put("message","删除失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/findCustomerById")
    public String findCustomerById(Integer id){
        System.out.println(id);
        Map<String,Object> map = new HashMap<String,Object>();
        System.out.println(customerService.findCustomerByID(id));
        if(customerService.findCustomerByID(id)!=null){
            map.put("success",true);
            map.put("message","查找成功");
            map.put("info",customerService.findCustomerByID(id));
        }else{
            map.put("success",false);
            map.put("message","查找失败");
        }
        return JSON.toJSONString(map);
    }

}
